
#include "zos_config.h"

#ifdef BOARD_USING_FLASH_DEMO
#include  "zos.h"
#define  USER_DATA_FLASH_BASE1		ZOS_FLASH_USER_NV_BASE
#define  USER_DATA_FLASH_LEN1		2*ZOS_FLASH_SECTOR_LENGTH

#define  USER_DATA_FLASH_BASE2		(USER_DATA_FLASH_BASE1+USER_DATA_FLASH_LEN1)
#define  USER_DATA_FLASH_LEN2		2*ZOS_FLASH_SECTOR_LENGTH

zos_uint8_t demo_flash_buf[100] = "This is a string used to test flash,1234567890.";
/*
为了保证flash的使用年限，用户应尽量减小写flash的次数，如果不同的数据在多处发生变化，可以在需要保存时在同一个接口一次性写入。
*/
void  write_user_nv_demo(zos_uint32_t addr)
{
	/* 磨损接口用户可用空间为 ZOS_FLASH_FTL_AVAILABLE_SIZE ,写入数据或者读数据，请不要超过 ZOS_FLASH_FTL_AVAILABLE_SIZE*/
	if(zos_flash_ftl_write(addr,0,(zos_uint8_t *)demo_flash_buf,strlen(demo_flash_buf)) != ZOS_EOK)
	{
		zos_printf("zos_flash_ftl_write error\r\n");
	}
	
}

void  read_user_nv_demo(zos_uint32_t addr)
{
	zos_uint8_t *g_user_cloud_data_demo = zos_malloc(100);
	memset(g_user_cloud_data_demo,0,100);
	if(zos_flash_ftl_read(addr,g_user_cloud_data_demo,strlen(demo_flash_buf)) != ZOS_EOK)
	{
		zos_printf("zos_flash_ftl_read error!!!\r\n");
	}
	else zos_printf("flash read data is:%s\r\n",g_user_cloud_data_demo);
}

void  init_user_flash()
{
	//数据长度为2*ZOS_FLASH_SECTOR_LENGTH,表示两块0x1000的flash参与磨损，交替写入。
	zos_flash_regist_ftl_req(USER_DATA_FLASH_BASE1,USER_DATA_FLASH_LEN1);
	zos_flash_regist_ftl_req(USER_DATA_FLASH_BASE2,USER_DATA_FLASH_LEN2);
}


int demo_flash(int argc, char *argv[])
{
	zos_uint32_t i;
	static zos_uint8_t state=0;
    /*接收到的参数小于3个*/
    if (argc < 2)
    {
        zos_printf("Usage: demo_flash write or read\r\n");
		zos_printf("Like: demo_flash  init\r\n");
        zos_printf("Like: demo_flash  write\r\n");
		zos_printf("Like: demo_flash  read\r\n");
        return ZOS_ERROR;
    }
	if(strstr(argv[1],"init"))
	{
		if(state == 0)
		{
			state=1;
			init_user_flash();
		}
		
	}
	else if(strstr(argv[1],"write"))
	{
		write_user_nv_demo(USER_DATA_FLASH_BASE1);
		write_user_nv_demo(USER_DATA_FLASH_BASE2);
	}
	else if(strstr(argv[1],"read"))
	{
		read_user_nv_demo(USER_DATA_FLASH_BASE1);
		read_user_nv_demo(USER_DATA_FLASH_BASE2);
	}
	else
	{
		zos_printf("cmd error\r\n");
		zos_printf("Like: demo_flash  init\r\n");
        zos_printf("Like: demo_flash  write\r\n");
		zos_printf("Like: demo_flash  read\r\n");
        return ZOS_ERROR;

	}

	return ZOS_EOK;
}

#include "shell.h"

SHELL_EXPORT_CMD(demo_flash,demo_flash, flash demo);

#endif




















